﻿using QD.SQLHelper;
using QD.Tools;
using QD_Clients.Code.DbContent;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QD_Clients
{
    public partial class SysUpgrade : BaseForm
    {
        public SysUpgrade()
        {
            InitializeComponent();
            this.gridList.InitGrid();
            this.dataGridView1.InitGrid();
        }

        private void SysUpgrade_Load(object sender, EventArgs e)
        {
            QDDevPlat_V4 db = new QDDevPlat_V4();
            DataTable dt = db.Query("select * from QDDataBase");
            this.gridList.DataSource = dt;

        }
        /// <summary>
        /// 配置库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ts_Update_Click(object sender, EventArgs e)
        {
            string sql = this.SQLContext.Text;
            if (string.IsNullOrEmpty(sql))
            {
                PromptHelper.Prompt("请输入升级的SQL！");
            }
            else
            {
                DataTable dt = this.gridList.DataSource as DataTable;
                if (dt != null && dt.Rows.Count > 0)
                {
                    DialogResult r = PromptHelper.PromptOkCancel("确认为所有配置数据库执行输入的SQL？");
                    if (r == DialogResult.OK)
                    {
                        this.dataGridView1.Rows.Clear();

                        foreach (DataRow dr in dt.Rows)
                        {
                            try
                            {
                                DbConfig.Instance.DbConfigName = dr["ConfigDataBase"].ToString();
                                DbConfigData db = new DbConfigData();
                                db.ExecuteNonQuery(sql);
                                this.dataGridView1.Rows.Add("执行成功:[" + dr["QfName"].ToString() + "]");
                            }
                            catch (Exception ex)
                            {
                                this.dataGridView1.Rows.Add("执行失败:[" + dr["QfName"].ToString() + "]原因：" + ex.Message);
                            }
                        }
                        PromptHelper.Prompt("执行结束！");
                    }
                }
                else
                {
                    PromptHelper.Prompt("请先连接数据库！");
                }
            }
        }
        /// <summary>
        /// 主数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string sql = this.SQLContext.Text;
            if (string.IsNullOrEmpty(sql))
            {
                PromptHelper.Prompt("请输入升级的SQL！");
            }
            else
            {
                DataTable dt = this.gridList.DataSource as DataTable;
                if (dt != null && dt.Rows.Count > 0)
                {
                    DialogResult r = PromptHelper.PromptOkCancel("确认为所有主数据库执行输入的SQL？");
                    if (r == DialogResult.OK)
                    {
                        this.dataGridView1.Rows.Clear();
                        List<string> list = new List<string>();
                        foreach (DataRow dr in dt.Rows)
                        {
                            string key = dr["MasterDataBase"].ToString();
                            if (!list.Contains(key))
                            {
                                list.Add(key);
                            }
                        }

                        foreach (var item in list)
                        {
                            try
                            {
                                DbConfig.Instance.DbMasterName = item;
                                DbData db = new DbData();
                                db.ExecuteNonQuery(sql);
                                this.dataGridView1.Rows.Add("执行成功:[" + item + "]");
                            }
                            catch (Exception ex)
                            {
                                this.dataGridView1.Rows.Add("执行失败:[" + item + "]原因：" + ex.Message);
                            }
                        }
                        PromptHelper.Prompt("执行结束！");
                    }
                }
                else
                {
                    PromptHelper.Prompt("请先连接数据库！");
                }
            }
        }

    }
}
